package ICM.client;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

import common.CommandMessage;
import common.Request;
import common.TextMessage;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JEditorPane;

/**
 * Shows assessor window
 * allow to get request data, assigned by system, and superior on request
 *
 */
public class AssessorUI extends JPanel implements ActionListener,Observer {

	private Client c;
	private ClientUI ui;
	private String IDR;
	private JLabel lblidRequest;
	private JLabel lblDateSend;
	private JLabel lblDepartment;
	private SystemEngineerUI SE;
	private Request request;
	private JLabel lblCommentsText;
	private JLabel lblReason;
	private JLabel lblDescription;
	private JTextField textDetermination;
	private JLabel lblPlace;
	private JTextArea textDescOfAssessor;
	private JTextArea textResult;
	private JTextArea textConsAndRisks;
	private JButton btnSaveAssessorReport;
	private JButton btnSaveAssessorReport1;
	private JButton btnCommit;
	private JButton btnBack;
	private boolean Result = false;
	private String[] assessorReport;
	private boolean StageFinished;

	/**
	 * Constructor - Build Assessor form
	 * Create the panel.
	 */
	public AssessorUI(final Client c,final ClientUI ui) {
		
		this.c=c;
		this.ui=ui;
		setLayout(null);
		c.addObserver(this);
				
		btnSaveAssessorReport1 = new JButton("Save Chenges ");
		btnSaveAssessorReport1.setBounds(220, 540, 214, 46);
		btnSaveAssessorReport1.addActionListener(this);
		add(btnSaveAssessorReport1);
		btnSaveAssessorReport1.setVisible(false);
		
		btnSaveAssessorReport = new JButton("Save Chenges ");
		btnSaveAssessorReport.setBounds(220, 540, 214, 46);
		btnSaveAssessorReport.addActionListener(this);
		add(btnSaveAssessorReport);
		btnSaveAssessorReport.setVisible(false);
		
		btnCommit = new JButton("Commit");
		btnCommit.addActionListener(this);
		btnCommit.setBounds(617, 563, 108, 23);
		add(btnCommit);
		btnCommit.addActionListener(this);
		btnCommit.setVisible(false);

		btnBack = new JButton("Back");
		btnBack.setBounds(10, 563, 89, 23);
		btnBack.addActionListener(this);
		add(btnBack);
		btnBack.setVisible(false);
	
		CommandMessage msg11 = new CommandMessage("Get request data",new Integer[] {Integer.parseInt(SE.info[0])});
		try {
			c.sendToServer(msg11);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
		    Thread.sleep(200);
		} catch(InterruptedException ex) {
		    Thread.currentThread().interrupt();
		}
		
		CommandMessage msg2 = new CommandMessage("Get assessor report data",new Integer[] {Integer.parseInt(SE.info[0])});
		try {
			c.sendToServer(msg2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
		    Thread.sleep(200);
		} catch(InterruptedException ex) {
		    Thread.currentThread().interrupt();
		}
		
		if(SE.info[2].equals("Active")){
		
		btnSaveAssessorReport1.setVisible(true);
		btnBack.setVisible(true);
			
		JLabel lblMeaningfulAssessment = new JLabel("Meaningful assessment");
		lblMeaningfulAssessment.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblMeaningfulAssessment.setBounds(276, 11, 264, 35);
		add(lblMeaningfulAssessment);
				
		JLabel lblDetermination = new JLabel("Determination of the stage");
		lblDetermination.setBounds(24, 315, 150, 14);
		add(lblDetermination);
				
		JLabel lblIdrequest = new JLabel("idRequest");
		lblIdrequest.setBounds(26, 77, 63, 14);
		add(lblIdrequest);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setBounds(26, 102, 98, 14);
		add(lblDescription);
		
		JLabel lblReasonOfRequest = new JLabel("Reason of request");
		lblReasonOfRequest.setBounds(485, 102, 108, 14);
		add(lblReasonOfRequest);
		
		JLabel lblComments = new JLabel("Comments");
		lblComments.setBounds(26, 216, 98, 14);
		add(lblComments);
		
		JLabel lblDateOfSend = new JLabel("Date of send request");
		lblDateOfSend.setBounds(254, 77, 124, 14);
		add(lblDateOfSend);
		
		lblidRequest = new JLabel();
		lblidRequest.setBounds(107, 74, 86, 20);
		add(lblidRequest);
		lblidRequest.setText(SE.info[0]);
		
		lblDateSend = new JLabel();
		lblDateSend.setBounds(376, 74, 86, 20);
		add(lblDateSend);		
		String s1;
		Date d1;
		d1=request.getdateRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		s1=d1.toString();
		lblDateSend.setText(s1);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(170, 210, 264, 84);
		add(scrollPane_2);
		
		lblCommentsText = new JLabel();
		scrollPane_2.setViewportView(lblCommentsText);
		lblCommentsText.setText(request.getComments());
		
		JLabel lblDepartment = new JLabel("Department");
		lblDepartment.setBounds(527, 77, 89, 14);
		add(lblDepartment);
		
		lblDepartment = new JLabel();
		lblDepartment.setBounds(600, 74, 158, 20);
		add(lblDepartment);
		lblDepartment.setText(request.getDepartment());
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(606, 96, 221, 109);
		add(scrollPane_1);
		
		lblReason = new JLabel();
		scrollPane_1.setViewportView(lblReason);
		lblReason.setText(request.getReasons());
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(170, 96, 264, 109);
		add(scrollPane);
		
		lblPlace = new JLabel();
		lblPlace.setBounds(232, 337, 202, 20);
		add(lblPlace);		
		lblPlace.setText(request.getDepartment());
		
		lblDescription = new JLabel();
		scrollPane.setViewportView(lblDescription);
		lblDescription.setText(request.getDescription());		
		
		textDetermination = new JTextField();
		textDetermination.setBounds(232, 312, 202, 20);
		add(textDetermination);
		textDetermination.setColumns(10);
		textDetermination.setText(assessorReport[3]);
		

		
		
		
		}
		
		if(SE.info[2].equals("Working")){
			
			btnSaveAssessorReport.setVisible(true);
			btnBack.setVisible(true);
			btnCommit.setVisible(true);
			
			JLabel lblMeaningfulAssessment = new JLabel("Meaningful assessment");
			lblMeaningfulAssessment.setFont(new Font("Tahoma", Font.PLAIN, 20));
			lblMeaningfulAssessment.setBounds(276, 11, 264, 35);
			add(lblMeaningfulAssessment);
					
			JLabel lblDetermination = new JLabel("Determination of the stage");
			lblDetermination.setBounds(24, 315, 150, 14);
			add(lblDetermination);
			
			JLabel lblPlace = new JLabel("Place");
			lblPlace.setBounds(24, 340, 46, 14);
			add(lblPlace);
			
			JLabel lblIdrequest = new JLabel("idRequest");
			lblIdrequest.setBounds(26, 77, 63, 14);
			add(lblIdrequest);
			
			JLabel lblDescription = new JLabel("Description");
			lblDescription.setBounds(26, 102, 98, 14);
			add(lblDescription);
			
			JLabel lblReasonOfRequest = new JLabel("Reason of request");
			lblReasonOfRequest.setBounds(485, 102, 108, 14);
			add(lblReasonOfRequest);
			
			JLabel lblComments = new JLabel("Comments");
			lblComments.setBounds(26, 216, 98, 14);
			add(lblComments);
			
			JLabel lblDateOfSend = new JLabel("Date of send request");
			lblDateOfSend.setBounds(254, 77, 124, 14);
			add(lblDateOfSend);
			
			lblidRequest = new JLabel();
			lblidRequest.setBounds(107, 74, 86, 20);
			add(lblidRequest);
			lblidRequest.setText(SE.info[0]);
			
			lblDateSend = new JLabel();
			lblDateSend.setBounds(376, 74, 86, 20);
			add(lblDateSend);		
			String s1;
			Date d1;
			d1=request.getdateRequest();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
			s1=d1.toString();
			lblDateSend.setText(s1);
			
			JScrollPane scrollPane_2 = new JScrollPane();
			scrollPane_2.setBounds(170, 210, 264, 84);
			add(scrollPane_2);
			
			lblCommentsText = new JLabel();
			scrollPane_2.setViewportView(lblCommentsText);
			lblCommentsText.setText(request.getComments());
			
			JLabel lblDepartment = new JLabel("Department");
			lblDepartment.setBounds(527, 77, 89, 14);
			add(lblDepartment);
			
			lblDepartment = new JLabel();
			lblDepartment.setBounds(600, 74, 158, 20);
			add(lblDepartment);
			lblDepartment.setText(request.getDepartment());
			
			JScrollPane scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(606, 96, 221, 109);
			add(scrollPane_1);
			
			lblReason = new JLabel();
			scrollPane_1.setViewportView(lblReason);
			lblReason.setText(request.getReasons());
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(170, 96, 264, 109);
			add(scrollPane);
			
			lblDescription = new JLabel();
			scrollPane.setViewportView(lblDescription);
			lblDescription.setText(request.getDescription());
		
		lblPlace = new JLabel();
		lblPlace.setBounds(232, 337, 202, 20);
		add(lblPlace);		
		lblPlace.setText(request.getDepartment());
		
		JLabel lblDescriptionOfAssessor = new JLabel("Description Of Assessor");
		lblDescriptionOfAssessor.setBounds(24, 365, 169, 14);
		add(lblDescriptionOfAssessor);
		
		
		JLabel lblResult = new JLabel("Result");
		lblResult.setBounds(477, 315, 46, 14);
		add(lblResult);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(232, 360, 202, 109);
		add(scrollPane_3);
		
		textDescOfAssessor = new JTextArea();
		textDescOfAssessor.setRows(10);
		scrollPane_3.setViewportView(textDescOfAssessor);
		textDescOfAssessor.setText(assessorReport[0]);
		
		JScrollPane scrollPane_4 = new JScrollPane();
		scrollPane_4.setBounds(589, 310, 238, 109);
		add(scrollPane_4);
		
		textResult = new JTextArea();
		textResult.setRows(10);
		scrollPane_4.setViewportView(textResult);
		textResult.setText(assessorReport[1]);
		
		
		JLabel lblConstraintsandrisks = new JLabel("ConstraintsAndRisks");
		lblConstraintsandrisks.setBounds(442, 427, 124, 14);
		add(lblConstraintsandrisks);
		
		JScrollPane scrollPane_5 = new JScrollPane();
		scrollPane_5.setBounds(589, 422, 238, 109);
		add(scrollPane_5);
		
		textConsAndRisks = new JTextArea();
		textConsAndRisks.setRows(10);
		scrollPane_5.setViewportView(textConsAndRisks);
		textConsAndRisks.setText(assessorReport[2]);
<<<<<<< .mine
		
=======
>>>>>>> .r301
		}

	}
	
	/**
	 * Handle massage from Observable class in our case Client
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		String msg = "";
		String cmd = "";
		if (arg1 instanceof TextMessage)
			msg = ((TextMessage)arg1).ToDebugMessage();
		else if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if(ms.getCommand().equals("request data"))
				request = (Request) ms.getParams()[1];
			else if(ms.getCommand().equals("Assessor report added")){
				Result = Boolean.parseBoolean(ms.getParams()[1].toString());}
			else if(ms.getCommand().equals("report data")){
				assessorReport=(String[]) ms.getParams()[1];}
			else if(ms.getCommand().equals("Assessor duration added")){
				Result = Boolean.parseBoolean(ms.getParams()[1].toString());}
<<<<<<< .mine
			if(ms.getCommand().equals("CommitOfMaingfulAssessment")){
				Result = Boolean.parseBoolean(ms.getParams()[1].toString());}
		}
=======
			else if(ms.getCommand().equals("Stage finished"))
				StageFinished = Boolean.parseBoolean(ms.getParams()[1].toString());	
			}
>>>>>>> .r301
		else
			msg = arg1.toString();
	}

	/**
	 * Button click event handler
	 */
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource().equals(btnSaveAssessorReport)){
			try{
					int Aid = request.getIdRequest();
					String dep = request.getDepartment();
					String desc = textDescOfAssessor.getText().trim();
					String res = textResult.getText().trim();
					String ConAndRisk = textConsAndRisks.getText().trim();
					CommandMessage msg = new CommandMessage("Add Assessor Report",new Object[] {Aid,dep,desc,res,ConAndRisk});
					try {
						c.sendToServer(msg);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
					    Thread.sleep(800);

					} catch(InterruptedException ex) {
					    Thread.currentThread().interrupt();
					}
					if (Result)
						
						ui.ShowMessageBox("Information", "Report Added" ,JOptionPane.INFORMATION_MESSAGE);
					else
						ui.ShowMessageBox("Error", "Can't create report" ,JOptionPane.ERROR_MESSAGE);
					ui.SetContent(ui.Main_Screen);
				
				}
			catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
		};
		
		if(e.getSource().equals(btnSaveAssessorReport1))
		{
			try{
				int Aid = request.getIdRequest();
				String dep = request.getDepartment();
				int duration = Integer.parseInt(textDetermination.getText().trim());
					CommandMessage msg = new CommandMessage("Add Assessor duration",new Object[] {Aid,dep,duration});
					try {
						c.sendToServer(msg);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
					    Thread.sleep(800);

					} catch(InterruptedException ex) {
					    Thread.currentThread().interrupt();
					}
					if (Result)
						
						ui.ShowMessageBox("Information", "Report Added" ,JOptionPane.INFORMATION_MESSAGE);
					else
						ui.ShowMessageBox("Error", "Can't create report" ,JOptionPane.ERROR_MESSAGE);
					ui.SetContent(ui.Main_Screen);
				
				}
			catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
		};
		
		if(e.getSource().equals(btnBack)){
			try 
			{ui.SetContent(ClientUI.SystemEngineerWindow_Screen);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
		}
<<<<<<< .mine
		if(e.getSource().equals(btnCommit)){
			try
			{
				int Aid = request.getIdRequest();
				CommandMessage msg = new CommandMessage("AssessorCommit",new Integer[] {Aid});
				try {
					c.sendToServer(msg);
					} 
					catch (Exception e1)
					{
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				try {
				    Thread.sleep(800);
					} 
					catch(InterruptedException ex)
					{
				    Thread.currentThread().interrupt();
					}
				if (Result)
					
					ui.ShowMessageBox("Information", "Stage Ended" ,JOptionPane.INFORMATION_MESSAGE);
				
				ui.SetContent(ui.Main_Screen);
			
			}
		catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
=======
		else if(e.getSource().equals(btnCommit)){
			CommandMessage msg = new CommandMessage("Finish assessment stage",new String[] {SE.info[0]});
			try {
				c.sendToServer(msg);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
			    Thread.sleep(400);
			} catch(InterruptedException ex) {
			    Thread.currentThread().interrupt();
			}
			if (StageFinished)
				ui.ShowMessageBox("Information", "Stage finished" ,JOptionPane.INFORMATION_MESSAGE);
			else
				ui.ShowMessageBox("Error", "Can't finish stage" ,JOptionPane.ERROR_MESSAGE);
			}
		}
>>>>>>> .r301
		
	}